/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.pro.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.pro.entity.ProLog;
import com.thinkgem.jeesite.modules.pro.entity.Project;
import com.thinkgem.jeesite.modules.pro.entity.ProjectTask;
import com.thinkgem.jeesite.modules.pro.entity.TaskGroup;
import com.thinkgem.jeesite.modules.pro.service.ProLogService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;

/**
 * 项目日志Controller
 * @author fanqinglong
 * @version 2017-08-21
 */
@Controller
@RequestMapping(value = "${adminPath}/pro/proLog")
public class ProLogController extends BaseController {

	@Autowired
	private ProLogService proLogService;
	@Autowired
	private SystemService systemService;
	
	@ModelAttribute
	public ProLog get(@RequestParam(required=false) String id) {
		ProLog entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = proLogService.get(id);
		}
		if (entity == null){
			entity = new ProLog();
		}
		return entity;
	}
	
	@RequiresPermissions("pro:proLog:view")
	@RequestMapping(value = {"list", ""})
	public String list(ProLog proLog, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ProLog> page = proLogService.findPage(new Page<ProLog>(request, response), proLog); 
		model.addAttribute("page", page);
		model.addAttribute("proLog", proLog);
		String task = request.getParameter("task");
		if ("task".equals(task)) {
			return "modules/pro/proLogListTask";
		}
		return "modules/pro/proLogList";
	}

	@RequiresPermissions("pro:proLog:view")
	@RequestMapping(value = "form")
	public String form(ProLog proLog, Model model) {
		model.addAttribute("proLog", proLog);
		return "modules/pro/proLogForm";
	}

	@RequiresPermissions("pro:proLog:edit")
	@RequestMapping(value = "save")
	public String save(ProLog proLog, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, proLog)){
			return form(proLog, model);
		}
		proLogService.save(proLog);
		addMessage(redirectAttributes, "保存项目日志成功");
		return "redirect:"+Global.getAdminPath()+"/pro/proLog/?repage";
	}
	
	@RequiresPermissions("pro:proLog:edit")
	@RequestMapping(value = "delete")
	public String delete(ProLog proLog, RedirectAttributes redirectAttributes) {
		proLogService.delete(proLog);
		addMessage(redirectAttributes, "删除项目日志成功");
		return "redirect:"+Global.getAdminPath()+"/pro/proLog/?repage";
	}

	/**
	 * 查看详细数据内容
	 * @param proLog
	 * @return
	 */
	@RequiresPermissions("pro:proLog:view")
	@RequestMapping(value = "view")
	public String view(ProLog proLog, RedirectAttributes redirectAttributes,Model model){
		if (StringUtils.isEmpty(proLog.getId())) {
			addMessage(redirectAttributes, "数据异常！id不能为空");
		}
		ProLog resultLog = proLogService.get(proLog.getId());
		// 项目
		if (ProLog.PROJECT.equals(resultLog.getModule())) {
			Project data = (Project) JsonMapper.fromJsonString(resultLog.getData(), Project.class);
			if (null != data.getManager()) {
				User manager = systemService.getUser(data.getManager().getId());
				data.setManager(manager);
			}
			model.addAttribute("project", data);
			return "modules/pro/projectFormDisable";
		// 任务组
		}else if (ProLog.TASK_GROUP.equals(resultLog.getModule())) {
			TaskGroup data = (TaskGroup) JsonMapper.fromJsonString(resultLog.getData(), TaskGroup.class);
			model.addAttribute("taskGroup", data);
			return "modules/pro/taskGroupFormDisable";
		// 任务
		}else if (ProLog.TASK.equals(resultLog.getModule())) {
			ProjectTask data = (ProjectTask) JsonMapper.fromJsonString(resultLog.getData(), ProjectTask.class);
			if (null != data.getManager()) {
				User manager = systemService.getUser(data.getManager().getId());
				data.setManager(manager);
			}
			model.addAttribute("projectTask", data);
			return "modules/pro/projectTaskFormDisable";
		}
		return "redirect:"+Global.getAdminPath()+"/pro/proLog/?repage";
	}
}